package com.homelock.common.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.homelock.constants.ErrorSms;
import com.homelock.domain.sms.ActiveSms;
import com.homelock.domain.sms.AliyunProfile;
import com.pi.base.dto.result.ServiceResult;

public class AliyunProfileManager {

  @SuppressWarnings("unused")
  private static final Logger logger = LoggerFactory.getLogger(AliyunProfileManager.class);
  
  public static IAcsClient initializeProfile(AliyunProfile profile)
      throws ClientException{
     //设置超时时间-可自行调整
    System.setProperty("sun.net.client.defaultConnectTimeout", AliyunProfile.getDEFAULT_CONNECT_TIME_OUT());
    System.setProperty("sun.net.client.defaultReadTimeout", AliyunProfile.getDEFAULT_READ_TIME_OUT());
    
    //初始化ascClient,暂时不支持多region（请勿修改）
    IClientProfile clientProfile = DefaultProfile.getProfile(
        AliyunProfile.getEndPoint(), profile.getAccessKeyId(), profile.getAccessKeySecret());
    DefaultProfile.addEndpoint(AliyunProfile.getEndPoint(), AliyunProfile.getEndPoint(),
        profile.getProduct(), profile.getDomain());
    
    IAcsClient acsClient = new DefaultAcsClient(clientProfile);
    return acsClient;
  }
  
  public static ServiceResult<String> checkActiveSms(ActiveSms sms){
    if(null == sms){
      return ServiceResult.newFailResult(ErrorSms.ERR_EMPTY_SMS.getDesc(), 
          ErrorSms.ERR_EMPTY_SMS.getErrorCode());
    }else if(null == sms.getSourceId() || sms.getSourceId() <= 0){
      return ServiceResult.newFailResult(ErrorSms.ERR_UNKNOWN_SMSORI.getDesc(), 
          ErrorSms.ERR_UNKNOWN_SMSORI.getErrorCode());
    }else if(null == sms.getPhoneNumber()){
      return ServiceResult.newFailResult(ErrorSms.ERR_EMPTY_SMS.getDesc(), 
          ErrorSms.ERR_EMPTY_SMS.getErrorCode());
    }else if(null == sms.getSmsTemplateCode()){
      return ServiceResult.newFailResult(ErrorSms.ERR_EMPTY_SMS_TEMPLATE.getDesc(), 
          ErrorSms.ERR_EMPTY_SMS_TEMPLATE.getErrorCode());
    }
    return null;
  }
}

